<?php
session_start();

require_once('dataManager/definitions.php');
require_once('dataManager/DataManager.php');

//==============================================================================
//Class LoginManager
//==============================================================================
class LoginManager
{
	private $username_;
	private $password_;

	public function __construct($user, $pass)
	{
		$this->username_   = $user;
		$this->password_   = $pass;
	}

	public function __destruct()
	{
	}

	public function login($dbM)
	{	
		$condition = NB_ADMIN_USERS_COLUMN_NAME."="."'$this->username_'"." AND ".
					 NB_ADMIN_USERS_COLUMN_PASSWORD."="."'$this->password_'";

		$dbM->selectAllFromTable(NB_ADMIN_TABLE_USERS, $condition);

		if ($dbM->retrieveNumberOfResults() == 1)
		{
			return 1;
		}
		else
		{
			return 0;
		}
	}
}

// Main program
$dbM    = new DataManager("root", "amazon", "localhost", NB_DATABASE_ADMIN);
$loginM = new LoginManager($_POST['usr'], $_POST['pswd']);
$result = $loginM->login($dbM);

if($result == 1)
{
	$_SESSION["valid_login"] = true;
	$_SESSION["user"] = $_POST['usr'];

	$data = $dbM->retrieveResult();
	$_SESSION["uid"]  = $data[NB_ADMIN_USERS_COLUMN_UID];
}
else
{
	$_SESSION["valid_login"] = false;
	$_SESSION["user"] = "Not logged on";
	$_SESSION["uid"]  = (-1);
}
// 1 if success - 0 if failed login for visual display
echo json_encode($result); 



?>
